package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.model.CompleteModel;
import com.zahb.qadx.model.Curriculum;
import com.zahb.qadx.ui.activity.CurriculumDetailsActivity;
import com.zahb.qadx.util.ImageLoaderKt;

import java.util.List;

/**
 * Created by： huwei.
 * Data： 2020-10-21.
 * Time： 15:04.
 * Desc：课程adapter
 */
public class CourseTaskAdapter extends RecyclerView.Adapter<CourseTaskAdapter.CourseTaskViewHolder> {
    private Context mContext;
    private List<CompleteModel.CompleteData.CompleteList> list;

    public CourseTaskAdapter(Context mContext, List<CompleteModel.CompleteData.CompleteList> list) {
        this.mContext = mContext;
        this.list = list;
    }
    public void setList(List<CompleteModel.CompleteData.CompleteList> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public CourseTaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final CourseTaskViewHolder courseTaskViewHolder;
        if(viewType ==0){
            courseTaskViewHolder = new CourseTaskViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_complete_one, parent, false));
        }else{
            courseTaskViewHolder = new CourseTaskViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_complete_two, parent, false));
        }
        courseTaskViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CompleteModel.CompleteData.CompleteList course = list.get(courseTaskViewHolder.getAdapterPosition());
                Curriculum curriculum = new Curriculum(course.getId(), course.getName(), course.getSubName(),
                        course.getCoverImage(), course.getSnapshotId(), course.getCourseUserId());
                CurriculumDetailsActivity.actionStart(courseTaskViewHolder.itemView.getContext(), curriculum, CurriculumDetailsActivity.TYPE_FROM_TASK);
            }
        });
        return courseTaskViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CourseTaskViewHolder holder, int position) {
        holder.tv_name.setText(list.get(position).getName());
        holder.tv_desc.setText(!TextUtils.isEmpty(list.get(position).getSubName()) ? list.get(position).getSubName(): "该课程暂无简介");
        String coverImage = list.get(position).getCoverImage();
        ImageLoaderKt.loadImageSameCorners(holder.img_pic,coverImage,5f);
    }

    @Override
    public int getItemViewType(int position) {
        CompleteModel.CompleteData.CompleteList completeList = list.get(position);
        int courseType = completeList.getCourseType();
        if(courseType == 0){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        return list !=null ? list.size():0;
    }

    static class CourseTaskViewHolder extends RecyclerView.ViewHolder {
        private AppCompatImageView img_pic;
        private AppCompatTextView tv_name, tv_desc;

        public CourseTaskViewHolder(@NonNull View itemView) {
            super(itemView);
            img_pic = itemView.findViewById(R.id.img_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }
    }
}
